<template>
  <el-dialog 
    :close-on-click-modal="false" 
    :before-close="handleBeforeClose" 
    :visible="true" :title="title" width="70%">
    <el-form ref="form" :model="form" size="small" :disabled="disabled" label-width="150px">
      <div class="form-section-container">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="心眼垂直距离(cm)" prop="cycz">
              <el-input v-model="form.cycz" type="number"/>
            </el-form-item>
            <el-form-item label="安静血压(mmhg)" prop="ajxy">
              <el-input v-model="form.ajxy" type="number"/>
            </el-form-item>

            <el-form-item alt="4KPa抗荷正压呼吸血压(mmHg)" label="抗荷正压呼吸血压(mmHg)" prop="kfhxy">
              <el-input v-model="form.kfhxy" type="number"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-table :data="tableData" border>
              <el-table-column prop="parameter" label="双下肢蹬力">
              </el-table-column>
              <el-table-column label="左下肢">
                <template #default="scope">
                  <el-input v-model="scope.row.zxz" type="number"/>
                </template>
              </el-table-column>
              <el-table-column label="右下肢">
                <template #default="scope">
                  <el-input v-model="scope.row.yxz" type="number"/>
                </template>
              </el-table-column>
              <el-table-column label="持续时间">
                <template #default="scope">
                  <el-input  v-model="scope.row.cxsj" type="number"/>
                </template>
              </el-table-column>
            </el-table>
            <el-form-item label="下肢蹬力检测">
              <el-select
                v-model.number="form.xzdl"
                placeholder="请选择"
              >
                <el-option
                  v-for="[key, value] in xzdlStatus"
                  :key="value"
                  :label="value"
                  :value="key"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="PHP动作掌握程度">
            <el-select
              v-model="form.phpzw"
              placeholder="请选择"
            >
              <el-option
                v-for="[key, value] in skillLevels"
                :key="value"
                :label="value"
                :value="key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="曲线同步程度" alt="呼吸曲线与呼吸引导曲线同步程度">
            <el-select
              v-model="form.qxtb"
              placeholder="请选择"
              >
              <el-option
                v-for="[key, value] in syncLevels"
                :key="value"
                :label="value"
                :value="key"
                />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="建议">
        <el-input v-model="form.jy" type="textarea" />
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="8">
           <el-form-item label="评定结果">
            <el-select
              v-model="form.result"
              placeholder="请选择"
            >
              <el-option
                v-for="[key, value] in resultOptions"
                :key="value"
                :label="value"
                :value="key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="记录人" prop="createBy">
            <el-input v-model="form.createBy" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="记录日期" prop="createTime">
            <el-date-picker v-model="form.createTime" :picker-options="pickerOptions" type="date" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleCommit" :disabled="disabled">确认</el-button>

    </div>
  </el-dialog>
</template>

<script>
import {get, edit, add} from '@/api/trainKhzyhx'

import { ITEM_KHZYHX, RATE_RESULTS, SKILL_LEVELS, PASS_STATUS, SYNC_LEVELS} from '@/utils/constants'

import { table2Form, form2Table } from '@/utils/table'
import { validateInt } from '@/utils/validate'

export default {
  name: 'HpkhdzDialog',
  props: {
    trainId: {
      type: Number,
      default: -1,
      required: true
    },
    itemId: {
      type: Number,
      default: -1,
      required: true
    }
  },
  computed: {
    disabled() {
      return this.form.id != null && !this.checkPer(['admin']);
    }
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }]
      },
      tableData: [
        { parameter: '2Kpa', zxz: '', yxz: '', cxsj: ''},
        { parameter: '3Kpa', zxz: '', yxz: '', cxsj: ''},
        { parameter: '4Kpa', zxz: '', yxz: '', cxsj: ''}
      ],
      form: { id: null, trainId: null, trainItemId: null, cycz: null, ajxy: null, kfhxy: null, xzdl: null, zxz: null,
               yxz: null, cxsj:null, phpzw: null, qxtb: null, result: null, jy: null, createTime: null},
      title: '抗荷正压呼吸训练',
      syncLevels: SYNC_LEVELS,
      xzdlStatus: PASS_STATUS,
      skillLevels: SKILL_LEVELS,
      resultOptions: RATE_RESULTS,
      isLoading: false // 提交按钮的加载状态
    };
  },
  methods: {
    emit: function(event) {

    },
    handleBeforeClose(done) {
      this.$emit('close', this.itemId);
      if (done) {
        done(); // 调用done()来关闭对话框        
      }
    },
    handleCommit() {
      table2Form(this.tableData, this.form)
      console.log(this.tableData, this.form)
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.isLoading = true; // 提交按钮的加载状态
          this.form.trainId = this.trainId;
          this.form.trainItemId = this.itemId;
          if (this.form.id == null) {
            add(this.form).then(() => {
              this.$emit('close', this.itemId);
              this.isLoading = false;
            }).catch(() => {
              this.isLoading = false;
            });
          } else {
            edit(this.form).then(() => {
              this.$emit('close', this.itemId);
              this.isLoading = false;
            }).catch(() => {
              this.isLoading = false;
            });
          }
        }
      })
    },
    handleCancel() {
      this.$emit('cancel', this.itemId);
    }
  },
  created() {
    console.log(this.trainId, this.itemId);
    if (this.trainId !== -1) {
      this.shouldUpdate = false;

      get(this.trainId, this.itemId).then(response => {
        this.form = Object.assign(this.form, response);
        form2Table(this.form, this.tableData, 'parameter')


      });
    }
  },
  updated() {

    // 组件更新后执行的逻辑，比如获取数据
  },
  mounted() {
  }
}
</script>

<style scoped>
.form-section-container {
  border: 1px solid #DCDFE6;
  padding: 15px 20px;
  border-radius: 4px;
  margin-bottom: 15px;
}
.el-select {
  width: 100%;
}
.el-form-item--small.el-form-item {
    margin-bottom: 15px;
}
</style>